package com.bestcem.xm.user.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.user.constant.CommonConstant;
import com.bestcem.xm.user.dao.OrganizationWalletConfigDao;
import com.bestcem.xm.user.service.OrganizationWalletConfigService;
import com.bestcem.xm.user.service.dto.organization.OrganizationWalletConfigDTO;
import com.bestcem.xm.user.util.convert.OrganizationWalletConfigConvert;
import com.bestcem.xm.wallet.enums.OrgWalletConfigRechargeLimitEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公司钱包配置service实现
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/13 10:34
 */
@Slf4j
@Service
public class OrganizationWalletConfigServiceImpl implements OrganizationWalletConfigService {

    @Resource
    private OrganizationWalletConfigConvert organizationWalletConfigConvert;

    @Resource
    private OrganizationWalletConfigDao organizationWalletConfigDao;

    @Override
    public ServiceResult<String> insert(OrganizationWalletConfigDTO organizationWalletConfigDTO) {
        // 校验参数
        if (organizationWalletConfigDTO == null) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数不能为空");
        }
        if (!ObjectId.isValid(organizationWalletConfigDTO.getOrgId())) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id参数格式不正确");
        }
        if (organizationWalletConfigDTO.getRechargeLimit() == null) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "充值限制参数不能为空");
        }
        if (OrgWalletConfigRechargeLimitEnum.getByValue(organizationWalletConfigDTO.getRechargeLimit()) == null) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_FORMAT, "充值限制参数的值不合法");
        }
        // 新增并设置返回值
        String walletConfigId = organizationWalletConfigDao.insert(organizationWalletConfigConvert.dto2Do(organizationWalletConfigDTO));
        organizationWalletConfigDTO.setWalletConfigId(walletConfigId);
        return ServiceResult.success(walletConfigId);
    }

    @Override
    public ServiceResult<Integer> updateByOrgId(OrganizationWalletConfigDTO organizationWalletConfigDTO) {
        // 校验参数
        if (organizationWalletConfigDTO == null) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_FORMAT, "参数不能为空");
        }
        if (!ObjectId.isValid(organizationWalletConfigDTO.getOrgId())) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id参数格式不正确");
        }
        // 设置更新参数
        OrganizationWalletConfigDTO condition = new OrganizationWalletConfigDTO();
        condition.setOrgId(organizationWalletConfigDTO.getOrgId());
        // 更新
        return this.updateByCondition(organizationWalletConfigDTO, condition);
    }

    @Override
    public ServiceResult<Integer> updateByCondition(OrganizationWalletConfigDTO param, OrganizationWalletConfigDTO condition) {
        return ServiceResult.success(organizationWalletConfigDao
                .updateByCondition(organizationWalletConfigConvert.dto2Do(param), organizationWalletConfigConvert.dto2Do(condition)));
    }

    @Override
    public ServiceResult<OrganizationWalletConfigDTO> getByOrgId(String orgId) {
        // 校验参数
        if (!ObjectId.isValid(orgId)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id参数格式不正确");
        }
        // 设置查询参数并查询
        OrganizationWalletConfigDTO organizationWalletConfigDTO = new OrganizationWalletConfigDTO();
        organizationWalletConfigDTO.setOrgId(orgId);
        ServiceResult<List<OrganizationWalletConfigDTO>> serviceResult = this.findByCondition(organizationWalletConfigDTO);
        // 设置返回值
        if (CollectionUtils.isNotEmpty(serviceResult.getData())) {
            return ServiceResult.success(serviceResult.getData().get(0));
        }
        return ServiceResult.fail("查询公司钱包限制充值配置失败");
    }

    @Override
    public ServiceResult<List<OrganizationWalletConfigDTO>> listByOrgIds(List<String> orgIds) {
        // 校验参数
        if (CollectionUtils.isEmpty(orgIds)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "公司id列表参数格式不正确");
        }
        // 设置查询参数并查询
        OrganizationWalletConfigDTO organizationWalletConfigDTO = new OrganizationWalletConfigDTO();
        organizationWalletConfigDTO.setOrgIds(orgIds);
        ServiceResult<List<OrganizationWalletConfigDTO>> serviceResult = this.findByCondition(organizationWalletConfigDTO);
        // 设置返回值
        if (CollectionUtils.isNotEmpty(serviceResult.getData())) {
            return ServiceResult.success(serviceResult.getData());
        }
        return ServiceResult.fail("查询公司钱包限制充值配置失败");
    }

    @Override
    public ServiceResult<List<OrganizationWalletConfigDTO>> findByCondition(OrganizationWalletConfigDTO organizationWalletConfigDTO) {
        return ServiceResult.success(organizationWalletConfigConvert.dos2Dtos(organizationWalletConfigDao
                .selectBySelective(organizationWalletConfigConvert.dto2Do(organizationWalletConfigDTO), organizationWalletConfigDTO.getOrgIds())));
    }

    @Override
    public ServiceResult<Void> insertBatch(List<OrganizationWalletConfigDTO> walletConfigDTOList) {
        List<List<OrganizationWalletConfigDTO>> walletConfigBatch = ListUtils.partition(walletConfigDTOList, CommonConstant.PARTITION_SIZE);
        for (List<OrganizationWalletConfigDTO> configs : walletConfigBatch) {
            organizationWalletConfigDao.insertBatch(organizationWalletConfigConvert.dtos2Dos(configs));
        }
        return ServiceResult.success();
    }
}
